package com.gxa.controller.investment;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.investmentDto.BusinessTypeAllDto;
import com.gxa.dto.investmentDto.BusinessTypeDto;
import com.gxa.entity.investment.BusinessType;
import com.gxa.entity.investment.BusinessTypeChild;
import com.gxa.service.investment.BusinessTypeChildService;
import com.gxa.service.investment.BusinessTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 招商配置——行业类型 前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-15 10:58:56
 */
@Api(tags = "招商配置/行业类型")
@RestController
@RequestMapping("/business/type")
public class BusinessTypeController {

    @Autowired
    private BusinessTypeService businessTypeService;
    @Autowired
    private BusinessTypeChildService businessTypeChildService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("按条件查询接口")
    @GetMapping("/list")
    public Result query(String business,Integer pageNum, Integer pageSize){
        try {
            PageInfo result = businessTypeService.queryByBusiness(business, pageNum, pageSize);

            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("query failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子列表接口")
    @GetMapping("/list/child/{businessTypeCoding}")
    public Result queryByCoding(@PathVariable Integer businessTypeCoding){
        try {
            return Result.success(businessTypeChildService.queryByCoding(businessTypeCoding));
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("queryByCoding failed");
        }
    };

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("父增加接口")
    @PostMapping("/add")
    public Result add(@RequestBody BusinessType businessType){
        int results = businessTypeService.add(businessType);
        if (results == 1){
            return Result.success("add success");
        }else {
            return Result.failed("add failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子增加接口")
    @PostMapping("/add/child")
    public Result addChild(@RequestBody BusinessTypeChild businessTypeChild){
        int results = businessTypeChildService.add(businessTypeChild);

        if (results == 1){
            int a = businessTypeChild.getBusinessTypeCoding();
            List<BusinessTypeChild> list = businessTypeChildService.queryByCoding(a);
            return Result.success(list);
        }else {
            return Result.failed("add failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除接口")
    @DeleteMapping("/del/{id}}")
    public Result delete(@PathVariable Integer id){
        int results = businessTypeService.delete(id);
        if (results == 1){
            return Result.success("del success");
        }else {
            return Result.failed("del failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子删除接口")
    @DeleteMapping("/del/child/{id}}")
    public Result deleteChild(@PathVariable Integer id){
        int result = businessTypeChildService.delete(id);
        if (result == 1){
            return Result.success("del 子 success");
        }else {
            return Result.failed("del 子 failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("父编辑接口")
    @PutMapping("/update")
    public Result update(@RequestBody BusinessType businessType){
        int results = businessTypeService.update(businessType);
        if (results == 1){
            return Result.success("update success");
        }else {
            return Result.failed("update failed");
        }
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子编辑接口")
    @PutMapping("/update/child")
    public Result updateChild(@RequestBody BusinessTypeChild businessTypeChild){
        boolean result = businessTypeChildService.updateById(businessTypeChild);
        return Result.success("update 子 success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据Id查找接口")
    @PostMapping("/typeall")
    public Result queryById(@RequestParam String coding){
        try{
            List<BusinessTypeAllDto> list = this.businessTypeService.queryById(coding);
            if (list.size()==0){
                return Result.failed("没有数据");
            }else {
                return Result.success(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

}
